package com.hdax.dm.mapstructs;

import com.hdax.dm.dto.OrderListDto;
import com.hdax.dm.entity.order.DmOrder;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Mapper(componentModel = "spring")
public abstract class OrderListDtoMapper {

    protected String turnToString(Long orderType){
        if(orderType == 0L) return "待支付";
        if(orderType == -1L) return "支付超时";
        if(orderType == 2L) return "已支付";
        return null;
    }

    protected String calcTime(LocalDateTime createdTime){
        LocalDateTime endTime = createdTime.plusMinutes(15L);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTimeFormatter.format(endTime);
    }

    @Mappings({
            @Mapping(target = "orderType", expression = "java(turnToString(source.getOrderType()))"),
            @Mapping(target = "endTime", expression = "java(calcTime(source.getCreatedTime()))"),
            @Mapping(target = "sellTime", expression = "java(calcTime(source.getCreatedTime()))"),
            @Mapping(target = "num",source="totalCount")
    })
    public abstract OrderListDto toTarget(DmOrder source);

    public abstract List<OrderListDto> toTargetList(List<DmOrder> sources);
}
